JR = (function($){
	return {
		ajax : function(url, success, data){
			var options={
				url: url,
				type: 'post',
				data: data,
				success: function(data){
					success($.parseJSON(data));
				}
			};
			$.ajax(options);
		},
		url : function(url){
			return JR.statics.contextPath + '/' + url;
		},
		url2 : function(url){
			return JR.statics.contextPath + '/admin/' + url;
		}		
	};
})(jQuery);

JR.utils = function($){
	return {
		hideMore : function(els, length){
			els.each(function(){
				var objHtml = $(this).html();
				if(objHtml.length > length){
					$(this).html(objHtml.substring(0, length) + '...');
				}
			});
		},
		textarea : function(string){
			return string.replace(/\r\n/g,"<br\/>"); 
		}
	};
}(jQuery);

JR.statics = function($){
	return {
		init : function(){
			this.contextPath = $('#contextPath').val();
			this.id = $('#id').val();
		},
		setId:function(id){
			this.id = id;
			$('#id').val(id);
		}
	};
}(jQuery);

JR.uniform = function($){
	return {
		init : function(){
			$("input, textarea, button").uniform({
				fileDefaultText : '请选择图片',
				fileBtnText : '浏览...'
			});
		}
	};
}(jQuery);

(function($){
    var jw = {
        stripScripts: function(text){
            var scripts = '';
            var res = text.replace(/<script[^>]*>([\s\S]*?)<\/script>/gi, function(){
                var script = arguments[0];
                scripts += script + '\n';
                return '';
            });
            return {'html':res,'scripts':scripts};
        },
        stripCss: function(text){
            var css = '';
            var res = text.replace(/<link[^>]*>/gi, function(){
                var cs = arguments[0];
                css += cs + '\n';
                return '';
            });
            return {'html':res,'css':css};
        },
        processHTML: function(text){
            var match = text.match(/<body[^>]*>([\s\S]*?)<\/body>/i);
            text = (match) ? match[1] : text;
            return text;
        }
    };

    jQuery.fn.loadx = function( url, params, callback ){
		if ( typeof url !== "string" ) {
			return;
		// Don't do a request if no elements are being requested
		} else if ( !this.length ) {
			return this;
		}

		var off = url.indexOf(" ");
		if ( off >= 0 ) {
			var selector = url.slice(off, url.length);
			url = url.slice(0, off);
		}

		// Default to a GET request
		var type = "GET";

		// If the second parameter was provided
		if ( params ) {
			// If it's a function
			if ( jQuery.isFunction( params ) ) {
				// We assume that it's the callback
				callback = params;
				params = null;

			// Otherwise, build a param string
			} else if ( typeof params === "object" ) {
				params = jQuery.param( params, jQuery.ajaxSettings.traditional );
				type = "POST";
			}
		}

		var self = this;

        var rscript = /<script(.|\s)*?\/script>/gi;

        var onComplete = function(res, status){
            if ( status === "success" || status === "notmodified" ) {
                var htmlText = res.responseText;
                var res = jw.stripScripts(htmlText);
                htmlText = res.html;
                var scripts = res.scripts;

                res = jw.stripCss(htmlText);
                htmlText = res.html;
                var css = res.css;

                htmlText = jw.processHTML(htmlText);

                htmlText = css+htmlText;
                htmlText += scripts;
                if(selector){
                    htmlText = jQuery("<div />").append(htmlText).find(selector);
                }
               // alert(htmlText);
                self.html(htmlText);
            }

            if ( callback ) {
                setTimeout(function(){
                    self.each( callback, [res.responseText, status, res] );
                }, 0);
            }
        };
		// Request the remote document
		jQuery.ajax({
			url: url,
			type: type,
			dataType: "html",
			data: params,
            cache:false,
			complete: function( res, status ) {
                onComplete(res, status);
			}
		});

		return this;
    };

}(jQuery));

$(document).ready(function() {
	JR.statics.init();
});